<template>
  <div class="login-container">
    <!-- 背景动画元素 -->
    <div class="login-bg-animation">
      <div class="warehouse-icon"></div>
      <div class="box-icon"></div>
      <div class="forklift-icon"></div>
    </div>
    
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
      <!-- 系统标题和logo -->
      <div class="title-container">
        <div class="system-logo">
          <img src="@/assets/login/ab1.png" alt="系统logo">
        </div>
        <h3 class="title">仓库进销存管理系统</h3>
        <p class="sub-title">高效 · 精准 · 智能</p>
      </div>

      <!-- 用户名输入 -->
      <el-form-item prop="username">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="username"
          v-model="loginForm.username"
          placeholder="请输入用户名"
          name="username"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>

      <!-- 密码输入 -->
      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="请输入密码"
          name="password"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>

      <!-- 登录按钮 -->
      <el-button 
        :loading="loading" 
        type="primary" 
        class="login-btn" 
        @click.native.prevent="handleLogin"
      >
        <svg-icon icon-class="login" class="btn-icon" />
        登录系统
      </el-button>

      <!-- 底部信息 -->
      <div class="footer-info">
        <p>© 2025 仓库进销存管理系统 v2.0</p>
        <p>技术支持：爱博科技有限公司</p>
      </div>
    </el-form>
  </div>
</template>

<script>
import { validUsername } from '@/utils/validate'

export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('请输入正确的用户名'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码长度不能少于6位'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: 'admin',
        password: '111111'
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/login', this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || '/' })
            this.loading = false
          }).catch(() => {
            this.loading = false
          })
        } else {
          console.log('验证失败!')
          return false
        }
      })
    }
  }
}
</script>



<style lang="scss" scoped>
// 在样式部分的最顶部添加这些变量定义
$bg: #5319a4;
$dark_gray: #889aa4;
$light_gray: #eee;  // 这就是缺失的变量
$primary_color: #409EFF;
$warehouse_blue: #2c7be5;
$cursor: #fff;
// 颜色变量
$bg: #8448d7;
$dark_gray: #889aa4;
$light_gray: #eee;
$primary_color: #409EFF;
$warehouse_blue: #2c7be5;
$cursor: #fff;

.login-container {
  min-height: 100vh;
  width: 100%;
  background-color: $bg;
  overflow: hidden;
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  background-image: linear-gradient(135deg, #77188e 0%, #0f172a 100%);

  .login-form {
    position: relative;
    width: 450px;
    padding: 50px 35px;
    margin: 0 auto;
    background: rgba(116, 23, 197, 0.8);
    border-radius: 10px;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
    backdrop-filter: blur(8px);
    z-index: 2;
    border: 1px solid rgba(255, 255, 255, 0.1);
  }

  // 标题区域
  .title-container {
    position: relative;
    margin-bottom: 40px;
    text-align: center;

     .logo-image {
    width: 80%;   // 相对容器宽度
    height: 80%;  // 相对容器高度
    max-width: 70px; // 限制最大尺寸
    object-fit: contain;
  }

    .title {
      font-size: 24px;
      color: $light_gray;
      margin: 0 0 10px;
      font-weight: 600;
      letter-spacing: 1px;
    }

    .sub-title {
      font-size: 14px;
      color: $dark_gray;
      margin: 0;
      letter-spacing: 2px;
    }
  }

  // 输入框图标
  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  // 显示密码按钮
  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
// system-logo
  // 登录按钮
  .login-btn {
    width: 100%;
    margin-bottom: 20px;
    height: 48px;
    background: $warehouse_blue;
    border: none;
    font-size: 16px;
    letter-spacing: 2px;
    transition: all 0.3s;

    &:hover {
      background: lighten($warehouse_blue, 5%);
      transform: translateY(-2px);
      box-shadow: 0 5px 15px rgba(44, 123, 229, 0.4);
    }

    .btn-icon {
      margin-right: 8px;
      width: 16px;
      height: 16px;
    }
  }

  // 底部信息
  .footer-info {
    text-align: center;
    font-size: 12px;
    color: rgba($dark_gray, 0.7);
    line-height: 1.6;

    p {
      margin: 5px 0;
    }
  }

  // 背景动画
  .login-bg-animation {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    overflow: hidden;
    z-index: 1;

    .warehouse-icon, .box-icon, .forklift-icon {
      position: absolute;
      opacity: 0.05;
      background-size: contain;
      background-repeat: no-repeat;
    }

    .warehouse-icon {
  background-image: url('https://example.com/path/to/warehouse.png');
}
.box-icon {
  background-image: url('https://example.com/path/to/box.png');
}
.forklift-icon {
  background-image: url('https://example.com/path/to/forklift.png');
}
  }
}

@keyframes float {
  0%, 100% {
    transform: translate(0, 0) rotate(0deg);
  }
  25% {
    transform: translate(-20px, -10px) rotate(2deg);
  }
  50% {
    transform: translate(10px, 15px) rotate(-2deg);
  }
  75% {
    transform: translate(15px, -5px) rotate(1deg);
  }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .login-container {
    .login-form {
      width: 90%;
      padding: 40px 25px;
    }
    
    .title-container {
      .title {
        font-size: 20px;
      }
      
      .sub-title {
        font-size: 12px;
      }
    }
    
    .login-bg-animation {
      .warehouse-icon, .box-icon, .forklift-icon {
        opacity: 0.03;
      }
    }
  }
}
</style>
